#ifndef COMMON_FACE_API_HELPER_H__
#define COMMON_FACE_API_HELPER_H__
#include <string>
#include <mutex>
#include "exception.h"
#include "baidu_face_api.h"

using namespace std;

//图片属性
typedef struct face_attr_
{
	float age;
	int expression;
	float expression_conf;
	int gender;
	float gender_conf;
	int glass;
	float glass_conf;
	int race;
	float race_conf;
}face_attr_t;

//图片质量
typedef struct face_quality_
{
	float bluriness;
	int illum;
	float occl_chin;
	float occl_l_contour;
	float occl_l_eye;
	float occl_mouth;
	float occl_nose;
	float occl_r_contour;
	float occl_r_eye;
}face_quality_t;



class ApiReturnError : public std::exception
{
public:
	ApiReturnError(const std::string& message):
		msg(message)
	{
	}

	const char* what() const noexcept override {
		return msg.c_str();
	}

private:

	std::string msg;
};

class FaceApiHelper 
{
public:
    static FaceApiHelper* getInstance() {
        //if( instance == NULL ) {
        //    instance = new FaceApiHelper();
        //}
        //return instance;
		//
		//下面是C++11的新写法，C++11中的static能保证instance是线程安全的
		static FaceApiHelper instance;
		return &instance;
    }


    int sdk_init(bool is_card =false);
	
	bool is_auth();

	//获取人脸属性
	face_attr_t face_attr_by_base64(const std::string& img_base64);
    //获取图片质量
	face_quality_t face_quality_by_base64(const std::string& img_base64);

	int track(std::vector<TrackFaceInfo> *& out, const cv::Mat &mat, int maxTrackObjNum);
	int track_by_base64(std::vector<TrackFaceInfo>* & out,const std::string& image,int maxTrackObjNum);
	int get_tracked_faces(std::vector<TrackFaceInfo> *& out);

	int get_face_feature_by_base64(const std::string& image,std::vector<float> &feature);
    int get_face_feature_by_filepath(const std::string& img_path,std::vector<float>& feature);
	
	float match_by_base64(const std::string& img1,const std::string& img2);

	float compare_feature(const std::vector<float>& f1,const std::vector<float>& f2);

private:
    FaceApiHelper();
    FaceApiHelper(const FaceApiHelper&);
    FaceApiHelper& operator=(const FaceApiHelper&);
    //static FaceApiHelper* instance;

	//tpye 1 base54 2 file path
	int get_feature(const std::string& img,int type,std::vector<float>& fs);

    BaiduFaceApi* api;
    std::mutex api_mt;

	
};

#endif

